﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Infusion.CRM.Attachments
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public MainForm(string[] args)
            : this()
        {
            ParseArgs(args);
            if (!String.IsNullOrEmpty(txtFileName.Text))
            {
                // If file is populated (from command args) execute and exit
                btnGo_Click(null, null);
                this.Close();
            }
        }

        private void ParseArgs(string[] args)
        {
            for (int i = 0; args.Length == 1 && i == 0 || i < args.Length - 1; i++)
            {
                if (args[i].Length == 2 && (args[i][0] == '-' || args[i][0] == '/'))
                {
                    switch (args[i][1])
                    {
                        case 'f':
                            txtFileName.Text = args[i + 1];
                            break;

                        case 'n':
                            txtEntityName.Text = args[i + 1];
                            break;

                        case 'k':
                            txtEntityKeyField.Text = args[i + 1];
                            break;

                        case 'i':
                            txtEntityGuid.Text = args[i + 1];
                            break;

                        case 'a':
                            txtAltIdName.Text = args[i + 1];
                            break;

                        case 'v':
                            txtAltIdValue.Text = args[i + 1];
                            break;

                        case 'c':
                            txtDescription.Text = args[i + 1];
                            break;

                        case 't':
                            txtTitle.Text = args[i + 1];
                            break;

                        case '?':
                            Help();
                            break;
                    }
                }
            }
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dialog = new OpenFileDialog();
                dialog.FileName = txtFileName.Text;
                if (DialogResult.OK == dialog.ShowDialog())
                {
                    txtFileName.Text = dialog.FileName;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            try
            {
                Verify();
                Guid noteId = new Helper(txtFileName.Text, txtEntityName.Text, txtEntityKeyField.Text, txtEntityGuid.Text, txtAltIdName.Text,
                    txtAltIdValue.Text, txtDescription.Text, txtTitle.Text).Attach();
                if (sender != null)
                {
                    // if sender is populated users has clicked the button, instead than automatic execution
                    MessageBox.Show("Done. Id = " + noteId.ToString(), "Done", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Verify()
        {
            if (string.IsNullOrEmpty(txtFileName.Text))
            {
                throw new ApplicationException("File Name cannot be empty");
            }
            if (string.IsNullOrEmpty(txtEntityName.Text))
            {
                throw new ApplicationException("Entity Name cannot be empty");
            }
            if (string.IsNullOrEmpty(txtEntityKeyField.Text))
            {
                throw new ApplicationException("Entity Key Field cannot be empty");
            }
            if (string.IsNullOrEmpty(txtEntityGuid.Text))
            {
                if (string.IsNullOrEmpty(txtAltIdName.Text))
                {
                    throw new ApplicationException("Alt Id Name must be specified if GUID is not specified");
                }
                if (string.IsNullOrEmpty(txtAltIdValue.Text))
                {
                    throw new ApplicationException("Alt Id Value must be specified if GUID is not specified");
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(txtAltIdName.Text))
                {
                    throw new ApplicationException("Alt Id Name cannot be specified if GUID is specified");
                }
                if (!string.IsNullOrEmpty(txtAltIdValue.Text))
                {
                    throw new ApplicationException("Alt Id Value cannot be specified if GUID is specified");
                }
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Help();
        }

        private void Help()
        {
            MessageBox.Show(
                "Rudimentary utility to load attachments in CRM 4.0. The primary entity (the object to attach the file to)" +
                " can be identified through its GUID or through any other text attribute (the Alternate ID).\r\n\t\n" +
                "The default values are set to attach files to Cases (incidents) using the case Title (Alternate ID) as identifier.\r\n\r\n" +
                "Can be launched from the command line, with the following parameters:\r\n\r\n" +
                "\t-?\tPrint this Help screen.\r\n" +
                "\t-f <file name>\r\n\t\tFile to attach.\r\n" +
                "\t-n <entity name>\r\n\t\tPrimary Entity (i.e. incident, account, etc.)\r\n" +
                "\t-k <entity Key Field>\r\n\t\tName of the key attribute (i.e. incidentid,\r\n\t\taccountid, etc.)\r\n" +
                "\t-i <entity GUID>\r\n\t\tGUID of the primary entity.\r\n" +
                "\t-a <entity alternate ID (Name)>\r\n\t\tAlternate attribute to identify\r\n\t\tthe primary object (name).\r\n" +
                "\t-v <entity alternate ID (Value)>\r\n\t\tAlternate attribute to identify\r\n\t\tthe primary object (value).\r\n" +
                "\t-c <description>\r\n\t\tDescription in the Note form.\r\n" +
                "\t-t <note title>\r\n\t\tTitle of the Note.",
                "Help", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    }
}
